package com.aaa.whj.controller;

import com.aaa.whj.entity.Scl;
import com.aaa.whj.entity.TabRecord;
import com.aaa.whj.entity.Tab_scl_record;
import com.aaa.whj.service.*;
import com.aaa.whj.service.impl.*;
import com.alibaba.fastjson.JSONArray;
import com.util.LayData;
import com.util.PrintJsonData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet(name = "indexServlet", value = "/indexServlet")
public class IndexServlet extends HttpServlet {
    private final IIndexService indexService=new IndexServiceImpl();
    private final ISclService sclService=new SclServiceImpl();
    private final IProblemService problemService=new ProblemServiceImpl();
/*    private final IRecordService recordService=new RecordServiceImpl();
    private final IPlanService planService=new PlanServiceImpl();*/
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if(method==null){
            query_scl_(req,resp);
        }else if(method.equals("sub")){
            record_submit(req,resp);
        }
    }

    private void record_submit(HttpServletRequest req, HttpServletResponse resp) {
        Map<String,String[]> parameterMap = req.getParameterMap();
        String data = parameterMap.get("data")[0];
        System.out.println("data = " + data);
        int i = indexService.addScl_topic(data);
        int j = indexService.updateRecord(data);
        int m = problemService.updateProblem(data);
        LayData success=new LayData(null);
        PrintJsonData.printData(resp,success);
    }

    private void query_scl_(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        long count = sclService.count(null);
        System.out.println("count = " + count);

        HttpSession session = req.getSession();
        Object id = session.getAttribute("id");
        System.out.println("id = " + id);
        List<Scl> scls = sclService.query_scl(null);
        System.out.println("scls = " + scls);
        LayData layData=LayData.success((int) count,scls);


        req.setAttribute("allsub",layData);
        req.setAttribute("recordid",id);
        req.getRequestDispatcher("viwes/whj/scl/index.jsp").forward(req,resp);
    }

}
